package com.kly.cashmall.module.mine.presenter;


import com.kly.cashmall.bean.BaseEntity;
import com.kly.cashmall.bean.MineInfoBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.utils.function.ToastUtil;
import com.kly.cashmall.widget.Loading.LoadingDialog;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 */
public class PersonalPresenter extends BaseViewPresenter<PersonalViewer> {

    public PersonalPresenter(PersonalViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }


    public void getUserInfo(){
        LoadingDialog.showLoading(getActivity());
        rxManager.add(Network.getApi().getUserInfo().subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<MineInfoBean>() {
            @Override
            public void accept(MineInfoBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().getUserInfoSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                ToastUtil.showAppToast(throwable.getMessage());

            }
        }));
    }

    public void saveUserHead(String path,String nickname) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("avatar", path);
        params.put("nickname", nickname);
        rxManager.add(Network.getApi().getUploadAvatar(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<BaseEntity>() {
            @Override
            public void accept(BaseEntity bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    ToastUtil.showToast(bean.getMsg());
                    getViewer().setUserPhoto(path);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                ToastUtil.showAppToast(throwable.getMessage());
            }
        }));
    }

}